package utils

import (
	"context"
	"time"

	"github.com/docker/docker/api/types/image"
	"github.com/docker/docker/client"
)

// 镜像信息结构体
type ImageInfo struct {
	ID          string    `json:"id"`
	Tags        []string  `json:"tags"`
	Created     time.Time `json:"created"`
	Size        int64     `json:"size"`
	VirtualSize int64     `json:"virtual_size"`
}

// 获取所有镜像
func ListImages(cli *client.Client) ([]ImageInfo, error) {
	images, err := cli.ImageList(context.Background(), image.ListOptions{})
	if err != nil {
		return nil, err
	}

	var result []ImageInfo
	for _, img := range images {
		result = append(result, ImageInfo{
			ID:          img.ID,
			Tags:        img.RepoTags,
			Created:     time.Unix(img.Created, 0),
			Size:        img.Size,
			VirtualSize: img.VirtualSize,
		})
	}
	return result, nil
}
